<template>
  <div class="login-container">
    <div class="login-box">
      <h2>登录</h2>
      <form @submit.prevent="handleLogin">
        <div class="input-group">
          <label for="username">用户名</label>
          <input v-model="username" type="text" id="username" placeholder="请输入用户名" required />
        </div>

        <div class="input-group">
          <label for="password">密码</label>
          <input
            v-model="password"
            type="password"
            id="password"
            placeholder="请输入密码"
            required
          />
          <p v-if="errorMessage" class="error-message">{{ errorMessage }}</p>
        </div>

        <div class="btn-group">
          <button type="submit" :disabled="isLoggingIn">
            {{ isLoggingIn ? '登录成功' : '登录' }}
          </button>
        </div>

        <div class="other-actions">
          <a @click.prevent="goToRegister" href="#">注册</a>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import axios, { AxiosError } from 'axios'
import { useRouter } from 'vue-router'

const username = ref('')
const password = ref('')
const router = useRouter()
const errorMessage = ref('')
const isLoggingIn = ref(false)

const handleLogin = async () => {
  console.log('测试')
  try {
    const response = await axios.post('api/user/login', null, {
      params: {
        username: username.value,
        password: password.value,
      },
    })

    const res = response.data
    console.log(response)

    if (res.message === '登录成功') {
      isLoggingIn.value = true
      // 跳转到主页面
      setTimeout(() => router.push('/home'), 400)
    } else {
      errorMessage.value = res.message
    }
  } catch (error: unknown) {
    if (error instanceof AxiosError) {
      errorMessage.value = '服务器错误，请稍后再试'
    }
  }
}

const goToRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
/* 登录框样式 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100vw;
}

/* 提示字体 */
.error-message {
  color: #d9534f; /* 温和的红色 */
  margin-top: 6px;
  font-size: 13.5px;
  font-weight: 500;
  font-family: 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
  transition: all 0.2s ease-in-out;
}

.login-box {
  background: white;
  padding: 50px 40px;
  border-radius: 12px;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.15);
  width: 100%;
  max-width: 420px;
  box-sizing: border-box;
  text-align: center;
}

h2 {
  margin-bottom: 30px;
  font-size: 26px;
  font-weight: bold;
  color: #333;
}

.input-group {
  margin-bottom: 24px;
  text-align: left;
}

.input-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  color: #555;
}

.input-group input {
  width: 100%;
  padding: 14px 12px;
  font-size: 16px;
  border: 1px solid #ccc;
  border-radius: 8px;
  box-sizing: border-box;
}

.input-group input:focus {
  border-color: #409eff;
  outline: none;
}

.btn-group button {
  width: 100%;
  padding: 14px;
  background-color: #409eff;
  color: white;
  font-size: 18px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.btn-group button:hover {
  background-color: #2f8eff;
}

.other-actions {
  margin-top: 20px;
}

.other-actions a {
  color: #409eff;
  text-decoration: none;
  font-size: 14px;
  margin: 0 10px;
}

.other-actions a:hover {
  text-decoration: underline;
}
</style>
